package icegps.com.bhmap.activity;

import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import icegps.com.bhmap.R;
import icegps.com.bhmap.bean.TargetPoint;
import icegps.com.bhmap.threadpool.ThreadPool;
import icegps.com.bhmap.utils.XYH;
import icegps.com.bhmap.view.MapView;
import timber.log.Timber;

public class MainActivity extends AppCompatActivity {


    @BindView(R.id.map_view)
    MapView mapView;

    double[] bPos = new double[3];
    double[] bECEF = new double[3];
    double[] rPos = new double[3];
    double[] rECEF = new double[3];
    double[] vECEF = new double[3];
    double[] enu = new double[3];
    XYH xyh;

    private int colors[] = {
            0xFF4cff90, 0xFF4effb4, 0xFF53ffe0, 0xFF57ffff,
            0xFF5de4ff, 0xFF5dd0ff, 0xFF60bfff, 0xFF6cb2f8,
            0xFF68a9fe, 0xFF46ff70, 0xFF41fe4a, 0xFF5bff40,
            0xFF77ff3d, 0xFF94ff36, 0xFFa9ff2f, 0xFFc5fe2d,
            0xFFdbfe2a, 0xFFebfe29, 0xFFfffc22, 0xFFfced1f,
            0xFFf8dd18, 0xFFfdec15, 0xFFf7c413, 0xFFf7b511,
            0xFFf4af05, 0xFFe99b01, 0xFFe29100, 0xFFda8700,
            0xFFd77e00, 0xFFc97500, 0xFFc26a00, 0xFFb36100,
            0xFFa75400};
    private Random random;
    private String assetsFilePath = "assets/data.txt";
    private int i;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        xyh = new XYH();
        random = new Random();

        ThreadPool.getInstance().execute(new Runnable() {
            @Override
            public void run() {
                InputStream is = getClass().getClassLoader().getResourceAsStream(assetsFilePath);
                InputStreamReader inputStreamReader = new InputStreamReader(is);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                try {
                    while (true) {
                        SystemClock.sleep(200);
                        String s = bufferedReader.readLine();
                        String[] data = s.split(",");
                        if (i == 0) {
                            bPos[0] = xyh.dmm2deg(Double.parseDouble(data[2])) * xyh.D2R;
                            bPos[1] = xyh.dmm2deg(Double.parseDouble(data[4])) * xyh.D2R;
                            bPos[2] = Double.parseDouble(data[9]) + Double.parseDouble(data[11]);
                            bECEF = xyh.pos2ecef(bPos);
                            i++;
                        }

                        rPos[0] = xyh.dmm2deg(Double.parseDouble(data[2])) * xyh.D2R;
                        rPos[1] = xyh.dmm2deg(Double.parseDouble(data[4])) * xyh.D2R;
                        rPos[2] = Double.parseDouble(data[9]) + Double.parseDouble(data[11]);

                        rECEF = xyh.pos2ecef(rPos);

                        vECEF[0] = rECEF[0] - bECEF[0];
                        vECEF[1] = rECEF[1] - bECEF[1];
                        vECEF[2] = rECEF[2] - bECEF[2];

                        enu = xyh.ecef2enu(bPos, vECEF);

                        int pointX = (int) (enu[0] * 100 );
                        int pointY = (int) (enu[1] * 100);
                        double height = (enu[2]);

                        TargetPoint targetPoint = new TargetPoint();
                        targetPoint.setHeight(height);
                        targetPoint.setPointX(pointX);
                        targetPoint.setPointY(pointY);

                        mapView.setTargetPoint(targetPoint);

                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (NumberFormatException e){
                    Timber.e("NumberFormatException");
                }
            }
        });

    }


}
